package cn.jilian.sys.web;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.jilian.common.domain.R;
import cn.jilian.common.dto.PageAndSort;
import cn.jilian.common.dto.PageResult;
import cn.jilian.entity.Admin;
import cn.jilian.entity.Meta;
import cn.jilian.entity.User;
import cn.jilian.meta.service.MetaService;
import cn.jilian.security.util.ShiroUtils;
import cn.jilian.sys.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private MetaService metaService;
	
	@Value("${app.user.module}")
	private Long userRankID;
	
	@ResponseBody
	@RequestMapping(value="/list",method= {RequestMethod.GET,RequestMethod.POST})
	@RequiresPermissions("system:user:list")
	public PageResult<User> userList(PageAndSort pageSortInfo,HttpSession session){
		Long unid=ShiroUtils.getUser().getUnitId();
		return userService.queryByUnid(pageSortInfo.getPageInfo(), pageSortInfo.getSortInfo(), unid);
	}

	@GetMapping("/mgr.html")
	public ModelAndView userMgr(HttpSession session) {
		ModelAndView mv=new ModelAndView("gui/tab");
		mv.addObject("title", "用户管理");
		try {
			Map<String,Object> metaData=metaService.getFullMetaById(userRankID);
			mv.addObject("metaData",metaData);
		}catch(Exception e) {
			e.printStackTrace();
		}
		mv.addObject("jsfile","sys.user_mgr");
		return mv;
	}
	
	
	
	@GetMapping("/admin/add")
	@RequiresPermissions("system:admin:add")
	public R addSysAdmin(Admin admin) {
		return R.ok;
	}
	
	@GetMapping("/admin/update")
	@RequiresPermissions("system:admin:update")
	public R updateSysAdmin(Admin admin) {
		return R.ok;
	}
	
	@GetMapping("/user/delete")
	@RequiresPermissions("system:user:delete")
	public R deleteUser(@RequestParam("id")Long id) {
		try {
			userService.deleteUser(id);
			return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}
	@RequiresAuthentication
	@GetMapping("/info/settings.html")
	public ModelAndView setUserInfo(HttpSession session) {
		ModelAndView mv=new ModelAndView("settings/user_info");
		User user=ShiroUtils.getUser();
		mv.addObject("user", user);
		return mv;
	}
	
	@RequiresAuthentication
	@GetMapping("/pass/reset.html")
	public ModelAndView resetPass(HttpSession session) {
		ModelAndView mv=new ModelAndView("settings/pass_reset");
		return mv;
	}
	
}
